/**
 * @description: sig生成器
 * @author: yuanjianpeng5
 * @createdate: 2020-01-09 18:42
 * @lastdate:
 */

package util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

public class ExpediaSignatureUtil {

    public static String calculationSignature(String apiKey, String secret) {
        Date date= new java.util.Date();
        Long timestamp = (date.getTime() / 1000);
        String signature = null;
        try {
            String toBeHashed = apiKey + secret + timestamp;
            MessageDigest md = MessageDigest.getInstance("SHA-512");
            byte[] bytes = md.digest(toBeHashed.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for(int i=0; i< bytes.length ;i++){
                sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
            }
            signature = sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String authHeaderValue = "EAN APIKey=" + apiKey +  ",Signature=" + signature + ",timestamp=" + timestamp;
//        System.out.println(authHeaderValue);
        return authHeaderValue;
    }

    public static void main(String[] args) {
        calculationSignature("1v8sptbnok1d0rit8hv24k97ia", "hipouhb455s5");
    }
}